from aip import AipSpeech
from playsound import playsound
import requests
import base64

APP_ID = '11322263'
API_KEY = 'gfExV6h1dp3YS5jlwS8P5GmQ'
SECRET_KEY = 'GwgtAyGWKilXOYrTTI5mmlyelGK81XUj'

# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
'''
银行卡识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
# 二进制方式打开图片文件
try:
    f = open('卡1.jpg', 'rb')
    img = base64.b64encode(f.read())
except:
    print("银行卡错误！！！\n请放入正确的银行卡")
    quit()
    exit()
params = {"image": img}
access_token = '24.e3316dfe75d7bab3a629ef056a6ba568.2592000.1595502276.282335-20455947'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)

A = (response.json())
B = A.get('result')
a1 = B.get('bank_card_number')
if B.get('bank_card_type') == 0:
    a2 = ('不能识别')
elif B.get('bank_card_type') == 1:
    a2 = ('借记卡')
else:
    a2 = ('信用卡')
a3 = ('银行卡号：' + B.get('bank_card_number') + '\n' + '银行卡类型：' + a2 + '\n' + '有效期：' + B.get(
    'valid_date') + '\n' + '所属银行：' + B.get('bank_name'))
print(a3)

result = aipSpeech.synthesis(a3, 'zh', 1, {'vol': 5, 'per': 4, 'spd': 9})
if not isinstance(result, dict):
    with open('auido.mp3', 'wb') as f:
        f.write(result)
playsound("auido.mp3")
